package com.example.android.helloactivity;



import android.app.Activity;
import android.app.AlertDialog;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableLayout;

public class main_activity extends Activity{
	
	//Draw Class
	ChessView myDraw;
	String winMsg;
	Boolean showWinMsg;
	
	//button
	
	Button back;
	Button quit;
	Button restart;
	
	OnClickListener huiqiListen;
	OnClickListener quitListen;
	OnClickListener restartListen;
	
	//RelativeLayout al;
	TableLayout tl;
	LinearLayout ll;
	
    String player_black="player_black";
    String player_white="player_white";
    int playWithPhone=1;//play with phone
    int selectColor=0;//stand for black
	WorkoutThread workThread;
	
	//add the zhongli ganying
	
	SensorManager sensorMgr;
	
	

    public boolean onTouchEvent (MotionEvent event){
		
    	//
    	//get the coordinate of x and y
    	//
        myDraw.x = (int)event.getX();  
        myDraw.y = (int)event.getY()-50;  
        
        //
        //only process the ACTION_UP event
        //
        switch(event.getAction()){  
            case MotionEvent.ACTION_DOWN: 
            	break;  
            case MotionEvent.ACTION_UP: 

            	//
            	//when phone thinking click is not allowed
            	//
	    		if(1 == myDraw.first.win || 1== myDraw.second.win || (1==playWithPhone&&true==myDraw.second.turn)){
            		
	    			AlertDialog.Builder mDialog = new AlertDialog.Builder(this);
            		mDialog.setTitle("Game Over!");
            		mDialog.setMessage("Please restart the game!");
            		mDialog.setIcon(R.drawable.btn_icon);
            		mDialog.setPositiveButton("OK", null);
            		mDialog.show();	    			
	    			break;
	    		}
    	    	
	    		//
	    		//judge if update the view
	    		//
    	    	if(myDraw.isUpdate()){
    	    		myDraw.postInvalidate();
    	    		if(true == myDraw.first.turn){
    	    			//between people and phone
    	    			if(myDraw.first.judgeWin(myDraw.first.history[myDraw.first.index-1].px,myDraw.first.history[myDraw.first.index-1].py)){
    	    				//draw the win dialogue
    	    				myDraw.first.win = 1;
    						showWinMsg = false;
    	            		AlertDialog.Builder mDialog = new AlertDialog.Builder(this);
    	            		mDialog.setTitle("Congratulations!");
    	            		mDialog.setMessage(myDraw.first.name+" win!");
    	            		mDialog.setIcon(R.drawable.win_icon);
    	            		mDialog.setPositiveButton("OK", null);
    	            		mDialog.show();
    	            		if(0==playWithPhone){
    	            			
    	            			myDraw.first.turn = !myDraw.first.turn;
    	        	    		myDraw.second.turn = !myDraw.second.turn;
    	            		}
    	            		break;
    	    			}
    	    			
        	    		myDraw.first.turn = !myDraw.first.turn;
        	    		myDraw.second.turn = !myDraw.second.turn;
        	    		
    	    			if(1==playWithPhone){
    	    				
    	    				workThread.WhereToSet();
    	    		    	//update the view
    	    				myDraw.postInvalidate();
    	    				
    	    				//judge if win ,play with phone
    	    				if(myDraw.second.judgeWin(myDraw.second.history[myDraw.second.index-1].px,myDraw.second.history[myDraw.second.index-1].py)){
    	    					//draw the win dialogue
    	    					myDraw.second.win = 1;
    	    					//show phone win message
    	                		showWinMsg=false;
    	                		AlertDialog.Builder mDialog = new AlertDialog.Builder(this);
    	                		mDialog.setTitle("Sorry!");
    	                		mDialog.setMessage("You not win!");
    	                		mDialog.setIcon(R.drawable.lost_icon);
    	                		mDialog.setPositiveButton("OK", null);
    	                		mDialog.show();
    	                		//do not break here
    	    				}
    	    				
    	    	    		myDraw.first.turn = !myDraw.first.turn;
    	    	    		myDraw.second.turn = !myDraw.second.turn;
    	    			}
    	    		}else{
    	    			//between people
    	    			if(myDraw.second.judgeWin(myDraw.second.history[myDraw.second.index-1].px,myDraw.second.history[myDraw.second.index-1].py)){
    	    				//draw the win dialogue
    	    				myDraw.second.win = 1;
    	    				showWinMsg=false;
    	            		AlertDialog.Builder mDialog = new AlertDialog.Builder(this);
    	            		mDialog.setTitle("Congratulations!");
    	            		mDialog.setMessage(myDraw.second.name+" win!");
    	            		mDialog.setIcon(R.drawable.win_icon);
    	            		mDialog.setPositiveButton("OK", null);
    	            		mDialog.show();
    	            		//do not break here
    	    			}
	    	    		myDraw.first.turn = !myDraw.first.turn;
	    	    		myDraw.second.turn = !myDraw.second.turn;
    	    		}
    	    	}else{
    	    		
    	    		myDraw.x = -1;
    	    		myDraw.y = -1;
    	    	}
            	break;
        }  
        return super.onTouchEvent(event);  
    }
    
	public void onCreate(Bundle savedInstanceState){
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.main_activity);
		Bundle bundle=this.getIntent().getExtras();
		playWithPhone=bundle.getInt("playWithPhone");
		selectColor=bundle.getInt("selectColor");
		player_black=bundle.getString("player_black");
		player_white=bundle.getString("player_white");
		showWinMsg = true;
        
		myDraw = new ChessView(this,playWithPhone,selectColor,player_black,player_white);
        
        LinearLayout ll=(LinearLayout)findViewById(R.id.linearlayout_21);
        ll.addView(myDraw);
        
		//
		//start a new thread to process where to set for computer,set ailevel
		//
        workThread = new WorkoutThread(myDraw,0);
        restartListen = new OnClickListener(){
        	public void onClick(View v){
        		
        		if(1 == playWithPhone && true == myDraw.second.turn){
        			return;
        		}
        		
        		winMsg = "";
        		myDraw.first.ReStart();
        		myDraw.second.ReStart();
        		myDraw.postInvalidate();
        		if(selectColor==0){
        			
        			myDraw.first.turn=true;
        			myDraw.second.turn=false;
        		}else{
        			
        			myDraw.second.situation[ChessView.QIPANSIZE/2][ChessView.QIPANSIZE/2]=1;
        			myDraw.second.history[0].px=ChessView.QIPANSIZE/2;
        			myDraw.second.history[0].py=ChessView.QIPANSIZE/2;
        			myDraw.second.history[0].invalid=1;
        			myDraw.second.index=1;
        			myDraw.postInvalidate();
        			myDraw.first.turn=true;
        			myDraw.second.turn=false;
        		}
        			
        		showWinMsg = true;
        	}
        };
        quitListen = new OnClickListener(){
        	public void onClick(View v){
        		
        		System.exit(0);
        	}
        };
        huiqiListen = new OnClickListener(){
        	public void onClick(View v){
        		
        		//protect the program when pc is workout the result
        		if(1 == playWithPhone && true == myDraw.second.turn){
        			return;
        		}
        		
        		//adjust when the first win and play with phone,the first should back 2 steps
        		if(1 == playWithPhone && true == myDraw.first.turn && 1 == myDraw.first.win){
        			
        			myDraw.first.huiQi();
        		}
        		myDraw.first.huiQi();
        		myDraw.second.huiQi();
        		
        		if(0==myDraw.first.index&&0==myDraw.second.index){
        			
	        		if(selectColor==0){
	        			
	        			myDraw.first.turn=true;
	        			myDraw.second.turn=false;
	        		}else{
	        			
	        			myDraw.second.situation[ChessView.QIPANSIZE/2][ChessView.QIPANSIZE/2]=1;
	        			myDraw.second.history[0].px=ChessView.QIPANSIZE/2;
	        			myDraw.second.history[0].py=ChessView.QIPANSIZE/2;
	        			myDraw.second.history[0].invalid=1;
	        			myDraw.second.index=1;
	        			myDraw.postInvalidate();
	        			myDraw.first.turn=true;
	        			myDraw.second.turn=false;
	        		}
        		}
        		myDraw.postInvalidate();
        		showWinMsg = true;
        	}
        };
        restart = (Button)findViewById(R.id.button_restart);
        quit = (Button)findViewById(R.id.button_quit_second);
        back = (Button)findViewById(R.id.button_back);
        //restart.setLayoutParams(LP_WW); 
        restart.setOnClickListener(restartListen);
        quit.setOnClickListener(quitListen);
        back.setOnClickListener(huiqiListen);
        
        //add the zhongli ganying
        
        sensorMgr = (SensorManager)getSystemService(SENSOR_SERVICE);
        Sensor sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        SensorEventListener lsn = new SensorEventListener(){
        	
        	public void onSensorChanged(SensorEvent e){
        		
        		float x,y,z;
        		x=e.values[SensorManager.DATA_X];
        		y=e.values[SensorManager.DATA_Y];
        		z=e.values[SensorManager.DATA_Z];
        		Log.d("DEBUG","x="+x+"  y="+y+"  z="+z);
        	}
        	
        	public void onAccuracyChanged(Sensor s,int accuracy){
        		
        	}
        };
        
        //register
        sensorMgr.registerListener(lsn,sensor,SensorManager.SENSOR_DELAY_GAME);
        
        //ImageButton iBtn=(ImageButton)findViewById(R.id.image_Button);
        //iBtn.setImageResource(R.drawable.btn_icon);
        
	}
}